package org.ckq.base;

import java.util.Iterator;

/**
 * Created by chenkaiqing on 17/8/16.
 */
public class List<E> implements Iterable<E>{
    private Object[] content = new Object[16];
    private int size ;
    public void put(E element){
        size++;
        if(size > content.length){
            extend();
        }
        content[size-1] = element;
    }

    private void extend() {
        Object[] newContent = new Object[content.length + 16];
        Array.copy(content,newContent);
        content = newContent;
    }

    public E get(int index){
        if(index > size-1){
            throw new IndexOutOfBoundsException();
        }
        E e = (E)content[index];
        return e;
    }
    public int size(){
        return size;
    }
    public List forEach(FunctionalInterface fi){
        List list = new List();
        return list;
    }

    @Override
    public Iterator<E> iterator() {
        class Itr<T> implements Iterator<T>{
            int cur = 0;

            @Override
            public boolean hasNext() {
                return cur > size-1 ? false : true;
            }

            @Override
            public T next() {
                T o = (T)get(cur);
                cur = ++cur;
                return o;
            }
        }
        return new Itr();
    }
}
